package copyengine.ui.tooltips.text
{
	import copyengine.ui.CESprite;
	import copyengine.ui.CESpriteEvent;
	import copyengine.ui.text.CETextManger;
	import copyengine.ui.tooltips.CEToolTipsManger;
	import copyengine.ui.tooltips.ICETooltipsSource;
	import copyengine.utils.GeneralUtils;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;

	public final class CETextToolTipsSource implements ICETooltipsSource
	{
		private var embedTarget:DisplayObjectContainer;
		private var dataWarp:CETextTooltipsDataWarp;


		public function CETextToolTipsSource()
		{
		}

		public function initialize(_embedTarget:DisplayObjectContainer, _textKey:String):void
		{
			embedTarget = _embedTarget;

			dataWarp = new CETextTooltipsDataWarp();
			dataWarp.text = CETextManger.instance.getText(_textKey);

			addListener();
		}

		public function isEqual(_source:ICETooltipsSource):Boolean
		{
			if (_source is CETextToolTipsSource)
			{
				return (_source as CETextToolTipsSource).embedTarget == this.embedTarget;
			}
			else
			{
				return false;
			}
		}

		private function addListener():void
		{
			if (embedTarget is CESprite)
			{
				GeneralUtils.addTargetEventListener(embedTarget, CESpriteEvent.DISPOSE, dispose);
			}
		}

		private function removeListener():void
		{
			GeneralUtils.removeTargetEventListener(embedTarget, CESpriteEvent.DISPOSE, dispose);
		}

		public function getContainer():DisplayObjectContainer
		{
			return embedTarget;
		}

		public function getData():*
		{
			return dataWarp;
		}

		private function dispose(e:Event):void
		{
			CEToolTipsManger.instance.unRegister(this);
			removeListener();
			embedTarget = null;
		}
	}
}
